﻿using System;
using System.Collections.Specialized;
using System.Data;
using System.Reflection;
using System.Web.UI;
using System.Xml;
using AMS.Core.Entities;
using AMS.Core.Manager;
using AMS.Web.BaseData.Helper;
using AMS.WorkBill.DataAccess;
using AMS.WorkBill.Entities;
using VitCode.BaseDataPageCreator;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;
using VitCode.Web.UI;
using VitCode.Web.UI.WebControls;

namespace AMS.Web.WorkBill
{
    public partial class WoAssetInfoSelect : UserControl
    {
        private readonly CreateControlHelper m_CreateCtlHlp = new CreateControlHelper();
        private WoPageBuilder m_WoPageBuilder;

        private UserDataSourceBuilder DataSourceBuilder
        {
            get { return (UserDataSourceBuilder)Session["DataSourceBuilder"]; }
            set { Session["DataSourceBuilder"] = value; }
        }

        public string SettingXmlPath
        {
            get { return AppDomain.CurrentDomain.BaseDirectory + "Setting\\WoSetting.xml"; }
        }

        public string ClassName
        {
            get { return hdnClassName.Value; }
            set { hdnClassName.Value = value; }
        }

        public string TypeName
        {
            get { return hdnTypeName.Value; }
            set { hdnTypeName.Value = value; }
        }

        public string WoIdVal
        {
            get { return hdnKeyVal.Value; }
            set { hdnKeyVal.Value = value; }
        }

        


        private string QueryCondition
        {
            get
            {
                if (ViewState["QueryCondition"] != null)
                    return ViewState["QueryCondition"].ToString();
                else
                    return "";
            }
            set { ViewState["QueryCondition"] = value; }
        }

        private string ReturnQueryStringValue(string keyString)
        {
            string strValue = Request.QueryString[keyString];
            if (strValue == null)
                strValue = "";
            return Convert.ToString(strValue);
        }


        protected override void OnInit(EventArgs e)
        {
            ClassName = ReturnQueryStringValue("ClassName");
            TypeName = ReturnQueryStringValue("TypeName");
            if (m_WoPageBuilder == null)
                m_WoPageBuilder = new WoPageBuilder(Page, SettingXmlPath);
            if (TypeName.Length > 0)
            {
                XmlNode listSqlNode = m_WoPageBuilder.GetSelectListSqlNode(ClassName,TypeName);
                if (listSqlNode != null)
                {
                    m_WoPageBuilder.CurrPageBuilder.SetPgListAttribute(pgList, listSqlNode, true, true, false);
                    if (m_WoPageBuilder.WoIsClose())
                    {
                        btnSelect.Enabled = false;
                        btnSelectList.Enabled = false;
                        
                    }
                }
            }
            base.OnInit(e);
        }

        public VitCode.Web.UI.ConditionSelector InnerConditionSelector
        {
            get { return ConditionSelector1; }
            set { ConditionSelector1 = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            pgList.DataSourceBuilderDelegate = new PaginationGrid.GridDataSourceBuilderDelegate(BindPgList);
            if (!IsPostBack)
            {
                m_CreateCtlHlp.SetDdlCondition(pgList, ddlCondition);
            }
        }

        private UserDataSourceBuilder BindPgList()
        {
            string sql = m_WoPageBuilder.GetSelectListSqlValue(ClassName,TypeName);
            sql = sql.Replace("\r\n", " ").Replace("\t", " ");
            sql += " " + QueryCondition;
            DataSourceBuilder = new UserDataSourceBuilder(sql, pgList);
            return DataSourceBuilder;
        }

        protected void ConditionSelector1_SelectCondition(SelectConditionEventArgs e)
        {
            QueryCondition = null;
            if (ConditionSelector1.SelectedConditionString.Length > 0)
                QueryCondition = " where " + ConditionSelector1.SelectedConditionString;
            
            pgList.BindGrid();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            QueryCondition = QueryHelper.GetQueryCondition(ddlCondition.SelectedValue, txtValue.Text, rbtAmount.Checked);
            
            pgList.BindGrid();
        }

        private void SelectAssetInfos(StringCollection scSelectIds)
        {
            WoSaveHelper saveHelper = new WoSaveHelper(Page, SettingXmlPath);
            WorkBillAccess acc = new WorkBillAccess();
            object objWoInfo = new WoPageBuilder(Page, SettingXmlPath).GetCurrSessionWoInfo(Page, WoIdVal);
            acc.BeginTransaction();
            try
            {
                if (scSelectIds != null && scSelectIds.Count > 0 && objWoInfo != null && objWoInfo is WorkBillInfo)
                {
                    saveHelper.CreateWoCommand(scSelectIds, (WorkBillInfo)objWoInfo, TypeName, acc);
                }
                acc.Commit();
            }
            catch (Exception ex)
            {
                acc.RollBack();
                throw ex;
            }
            finally
            {
                acc.CloseSession();
            }
            if (objWoInfo != null && objWoInfo is WorkBillInfo)
            {
                saveHelper.SetCurrSessionWoInfo(Page, (WorkBillInfo)objWoInfo);
            }
            
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            WoSaveHelper saveHelper = new WoSaveHelper(Page, SettingXmlPath);
            string strSelectIds = pgList.GetSelectedItem(1);
            if(string.IsNullOrEmpty(strSelectIds.Trim()))
            {
                Alert.MessageBox("没有选中资产!");
                return;
            }
            StringCollection scIds = saveHelper.ConvertToStringCollection(strSelectIds);
            SelectAssetInfos(scIds);
            Alert.MessageBox("资产选择成功!");
            pgList.BindGrid();
        }

        protected void pgList_OnGridItemSelect(object sender, PaginationGridItemSelectArgs e)
        {
            m_WoPageBuilder.ShowAssetInfoByAssetInfoId(e.SelectedDataKeyValue, Page);
            
        }

        protected void btnSelectList_Click(object sender, EventArgs e)
        {
            if(DataSourceBuilder==null || DataSourceBuilder.PrimaryKeys == null || DataSourceBuilder.PrimaryKeys.Count==0)
            {
                Alert.MessageBox("列表中没有资产!");
                return;
            }
            SelectAssetInfos(DataSourceBuilder.PrimaryKeys);
            Alert.MessageBox("资产选择成功!");
            pgList.BindGrid();
        }
    }
}